<template>
  <div>
    <TopFilter
      ref="topfilter"
      :list="topFilterList"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn='false'
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="potenCus_customer_driveManagementBq_index_01"
    >
      <template slot="controls" slot-scope="props">
        <!-- <span>当前待处理试驾/乘预约XX人</span>
        <el-button size="small" type="primary" icon="el-icon-search" @click="checkHandle">查看</el-button> -->
        <!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button> -->
        <multiuse-button size="small" type="primary" icon="el-icon-plus" :click="addHandle" :auth-list="auths" auth-mark="/api/cyxdms.clue/testDrive/saveTestDrivePlan">新增</multiuse-button>
        <multiuse-button size="small" type="primary" icon="el-icon-download" :loading="exportLoading" :click="exportHandle" :auth-list="auths" auth-mark="/api/cyxdms.clue/testDrive/exportTestDrivePlan">导出</multiuse-button>
        <!-- <el-button size="small" type="primary" :loading="exportLoading" @click="exportHandle">导出</el-button> -->
      </template>
    </FilterTable>
    <Drawer
      :visible.sync="visible"
      :title="dialogTitle"
      :width="960"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <DriveModalBq :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>
    <!-- 查看预约 -->
    <BaseDialog
      :visible.sync="visible_check"
      title="预约处理"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <checkInfo :type="handleType" :rowData="formData_check" @close="closeHandler_check" />
    </BaseDialog>
  </div>
</template>
<script>
import {
  searchDriveManagement,
  getEmployeeInfo,
  getDriveCarInfo,
  getDriveWayInfo,
  getDriveManagementExcelList
} from '@/api/potenCus/customer/driveManagement';
import { dictionary } from '@/utils/dictMixin';
import { formatDates } from '@/utils/validate';
import { getDataType } from '@/assets/js/auth';
import DriveModalBq from './DriveModalBq';
import checkInfo from './checkInfo'
import { authority } from '@/utils/authMixin';
import { exportExcelFile,formatdateByTime} from '@/utils/index';
export default {
  name: 'DriveManagement',
  mixins: [dictionary,authority],
  components: {
    DriveModalBq,
    checkInfo
  },
  data() {
    return {
      dataType: '',
      // isShowSe: true,
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      formData: {},
      visible_check: false,
      formData_check: {},
      dialogTitle: '',
      fetchApi: searchDriveManagement,
      columns: this.createTableColumns(),
      handleType: '',
      conf: { add: '新增', edit: '编辑', show: '明细', check: '查看预约' },
      exportLoading: false
    };
  },
  created() {
    this.getEmployeeList();
    this.getDriveCarList();
    this.getDriveWayList();
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'potentialCustomerName',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '客户手机号',
          fieldName: 'potentialCustomerPhone',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '展厅顾问',
          fieldName: 'userId',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '试驾车型',
          fieldName: 'testDriveId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '试驾路线',
          fieldName: 'wayId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '开始时间',
          style: { minWidth: '200px' },
          fieldName: 'driveSAt|driveEAt'
        },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.showHandle(props.row)}>
                  明细
                </el-button>
              </div>
              // <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
              //   编辑
              // </el-button>
                // <multiuse-button size="mini" divider="after" containerStyle={props.row.driveStatus !== 20211001 ? {color:'#D9D9D9'}:{}} type="text" onClick={() => this.editHandle(props.row)} auth-list={this.auths} auth-mark={'/api/cyxdms.clue/customerManage/testDrive/modifyTestDrive/${id}'}>
                //   编辑
                // </multiuse-button>
                // <multiuse-button size="mini" type="text" onClick={() => this.showHandle(props.row)}>
                //   明细
                // </multiuse-button>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '开始时间',
          dataIndex: 'driveSAt',
          sorter: true,
          width: 150,
          align: 'center',
          render: props =>{
            return <span>{ formatDates(props.row.driveSAt) } </span>
          }
        },
        {
          title: '结束时间',
          dataIndex: 'driveEAt',
          sorter: true,
          width: 150,
          align: 'center',
          render: props =>{
            return <span>{ formatDates(props.row.driveEAt) } </span>
          }
        },
        {
          title: '试驾车辆',
          dataIndex: 'carInfoName',
          minWidth: 260,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'potentialCustomerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户手机号',
          dataIndex: 'potentialCustomerPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '试驾人名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '试驾人手机号',
          dataIndex: 'mobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '展厅顾问',
          dataIndex: 'employeeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '本次里程(KM)',
          dataIndex: 'driveRunKm',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '试驾路线',
          dataIndex: 'wayName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.formData = {};
      this.showHandler();
    },
    checkHandle(){
      this.handleType = 'check';
      this.formData_check = {};
      this.visible_check = true;
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.formData.testDriveFileDTOList = row.drivingDisclaimer
      this.showHandler();
    },
    showHandle(row) {
      // console.log('查看', row);
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `试驾管理${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      console.log('搜索', val);
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    closeHandler_check(val, type) {
      this.visible_check = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async getEmployeeList() {
      const res = await getEmployeeInfo();
      console.log('销售顾问', res)
      if (res.resultCode === 200) {
        this.topFilterList.find(item => item.fieldName ==='userId').itemList = res.data.map(x => ({ value: x.employeeNo, text: x.employeeName }));
      }
    },
    async getDriveCarList() {
      const res = await getDriveCarInfo();
      console.log('试驾车型111', res)
      if (res.resultCode === 200) {
        this.topFilterList.find(item => item.fieldName ==='testDriveId').itemList = res.data.map(x => ({ value: x.testDriveId, text: x.carInfoName }));
      }
    },
    async getDriveWayList() {
      const res = await getDriveWayInfo();
      // console.log('试驾路线', res)
      if (res.resultCode === 200) {
        this.topFilterList.find(item => item.fieldName ==='wayId').itemList = res.data.map(x => ({ value: x.wayId, text: x.wayName }));
      }
    },
    //试驾管理导出
    async exportHandle(){
      let r = await this.$refs.topfilter.GET_FORM_DATA();
      let p = null;
      if (r != null && r.length > 1) {
        p = r[1];
      }
      this.exportLoading = true;
      getDriveManagementExcelList(p).then(res => {
        exportExcelFile(res.data, '试驾管理');
        this.exportLoading = false;
      });
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
